<?php

namespace App\Http\Controllers\User;

use App\Http\Requests\BrandRequest;
use App\Models\Brand;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class BrandController extends Controller
{
    public function index(){
        $brands = Brand::where('active','0')->where('partid',session()->get('doain')->id)->paginate(9);
        return view('akl.Admin.Brand_index',compact('brands'));
    }

    public function showIndex(){
        $brands = Brand::where('active','0')->where('partid',session()->get('doain')->id)->paginate(9);
        for ( $i=0;$i<count($brands);$i++ ){
            $brands[$i]['logo'] = $brands[$i]['logo']."?watermark/2/text/5be05ouJ5be05ouJ/font/5a6L5L2T/fontsize/500/fill/#000000=/dissolve/50/gravity/SouthEast/dx/0/dy/0";
        }
        return view('akl.Aikla.Merchant-index',compact('brands'));
    }

    public function create(){
        return view('akl.Admin.Brand_add');
    }

    public function store( BrandRequest $request ){
        $inp = Input::all();
        $inp['logo'] = $request->get('imgs')[0];
        $inp['partid'] = session()->get('doain')->id;
        $rel = Brand::create($inp);
        if($rel->wasRecentlyCreated) return back()->with('errors',"添加成功");
        return back()->with('errors',"添加失败，请重试");
    }

    public function destroy($id ){
        $rel = Brand::where('id',$id)->where('partid',session()->get('doain')->id)->update(['active'=>'1']);
        if($rel) return back()->with('errors',"删除成功");
        return back()->with('errors',"删除失败，请重试");
    }

    public function edit($id){
        $brand = Brand::where('id',$id)->where('partid',session()->get('doain')->id)->first();
        return view('akl.Admin.Brand_edit',compact('brand'));
    }
    public function update(Request $request,$id){
        $inp = array(
            'name'=>Input::all()['name'],
            'logo'=>$request->get('imgs')[0],
            'introduce'=>Input::all()['introduce'],
        );
        $rel = Brand::where('partid',session()->get('doain')->id)->where('id',$id)->update($inp);
        if($rel) return back()->with('errors',"更新成功");
        return back()->with('errors',"更新失败，请重试");
    }


}
